﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;



namespace CSP.Package.Controls
{
    public partial class CreateInternetSiteCollection : UserControl, IInstallationControl
    {
        private const string _executionPath = @"InternetPortal\CreateSiteCollection.bat";

        public CreateInternetSiteCollection()
        {
            InitializeComponent();
        }

        #region IInstallationControl Members

        private Installer _form;
        public Installer GetForm()
        {
            return _form;
        }

        public void SetForm(Installer installer)
        {
            _form = installer;
        }

        private string GetWebApplicationUrl()
        {
            if (RegistryAccess.PropertyExists(Registry.LocalMachine, Constants.CspRegistryKey, "CSPInternetWebApplication"))
                return RegistryAccess.GetStringProperty(Registry.LocalMachine, Constants.CspRegistryKey, "CSPInternetWebApplication");
            else
                return string.Empty;
        }

        private bool ValidateInputs()
        {
            if (txbTitle.Text.Length == 0)
            {
                MessageBox.Show(CSP.Package.labels.CspInstallError_TitleNoEmpty, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbTitle.Focus();
                txbTitle.Select();
                return false;
            }
            if (txbDescription.Text.Length == 0)
            {
                MessageBox.Show(CSP.Package.labels.CspInstallError_DescriptionNoEmpty, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbDescription.Focus();
                txbDescription.Select();
                return false;
            }
            if (txbUsername.Text.Length == 0)
            {
                MessageBox.Show(CSP.Package.labels.CspInstallError_UsernameNoEmpty, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbUsername.Focus();
                txbUsername.Select();
                return false;
            }
            if (txbEmail.Text.Length == 0)
            {
                MessageBox.Show(CSP.Package.labels.CspInstallError_EmailNoEmpty, CSP.Package.labels.CspInstallError_Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbEmail.Focus();
                txbEmail.Select();
                return false;
            }
            return true;
        }

        public bool Next()
        {
            try
            {
                if (ValidateInputs())
                {

                    GetForm().AddToLog("----------------------------------------------");
                    GetForm().AddToLog(CSP.Package.labels.CspInstallLog_IntranetSiteCollectionStart);

                    Process proces = new Process();
                    proces.StartInfo.FileName = _executionPath;
                    string arguments = string.Format(@"{0} ""{1}"" {2} {3} ""{4}""", new string[] { GetWebApplicationUrl(), txbTitle.Text, txbUsername.Text, txbEmail.Text, txbDescription.Text });

                    GetForm().AddToLog("   " + labels.CspInstallLog_Arguments + ": " + arguments);

                    proces.StartInfo.Arguments = arguments;
                    proces.StartInfo.UseShellExecute = false;
                    proces.StartInfo.RedirectStandardOutput = true;
                    proces.StartInfo.RedirectStandardError = true;
                    proces.Start();
                    string error = proces.StandardError.ReadToEnd();
                    string output = proces.StandardOutput.ReadToEnd();
                    proces.WaitForExit();
                    if (error != string.Empty)
                    {
                        throw new ApplicationException(error);
                    }
                    GetForm().AddToLog(output);

                    return true;
                }
                else
                    return false;
            }
            catch (Exception ex)
            {
                this.GetForm().ShowException(ex);
                throw ex;
            }
        }


        public void Previous()
        {
        }

        public void Skip()
        {
        }

        #endregion

        private void CreateInternetSiteCollection_Load(object sender, EventArgs e)
        {
            TranslateLiterals();
            txbUsername.Text = Environment.UserDomainName + "\\" + Environment.UserName;
            GetForm().AddToLog(CSP.Package.labels.CspInstallLog_IntranetSiteCollectionIniatiated);
        }

        private void TranslateLiterals()
        {
            richTextBox1.Text = CSP.Package.labels.CreateInternetWebApp_Intro;
            groupBox1.Text = CSP.Package.labels.SiteCollection_GroupBox;
            groupBox2.Text = CSP.Package.labels.SiteCollectionAdmin_GroupBox;
            lblUsername.Text = CSP.Package.labels.Username_Label;
            lblDescription.Text = CSP.Package.labels.Description_Label;
            lblEmail.Text = CSP.Package.labels.Email_Label;
            lblTitle.Text = CSP.Package.labels.Title_Label;
            this.txbTitle.Text = CSP.Package.labels.Internet_Label;
            this.txbDescription.Text = CSP.Package.labels.InternetDescription_Label;
        }

    }
}
